package cn.tedu.weibo02.controller;

import cn.tedu.weibo02.mapper.WeiboMapper;
import cn.tedu.weibo02.pojo.dto.WeiboDto;
import cn.tedu.weibo02.pojo.entity.Weibo;
import cn.tedu.weibo02.pojo.vo.UserVo;
import cn.tedu.weibo02.pojo.vo.WeiboDetailVo;
import cn.tedu.weibo02.pojo.vo.WeiboIndexVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/weibo")
public class WeiboController {
    @Autowired
    WeiboMapper mapper;

    @RequestMapping("/insert")
    public int insert(@RequestBody WeiboDto weiboDto, HttpSession session){
        UserVo userVo = (UserVo) session.getAttribute("user");
        if (userVo==null){
            return 2;
        }
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDto,weibo);
        weibo.setUserId(userVo.getId());
        weibo.setCreated(new Date());
        mapper.insert(weibo);
        return 1;
    }

    @RequestMapping("/select")
    public List<WeiboIndexVo> select(){
        return mapper.select();
    }

    @RequestMapping("selectById")
    public WeiboDetailVo selectById(int id){
        return mapper.selectById(id);
    }
}
